﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;
using eTManager.Models;

namespace eTManager.Controllers
{
    public class VersionController : Controller
    {
        [HttpGet]
        public ActionResult Check(string id)
        {
            if (!System.IO.File.Exists(VersionFile))
            {
                return Content("Error");
            }
            // read the text from VersionFile
            string content = System.IO.File.ReadAllText(VersionFile);
            return Content(content);
        }

        /*
         * 终端序列号；        用户名；      密码；
         * MachineCode=231003537;userId=000001;userPw=123456; 
         * 判断MachineCode、userId和userPw是否一致，如果一致
         * 返回1，否则返回-1；
         */
        [HttpGet]
        public int Login(string code, string userId, string pass)
        {
            if (String.IsNullOrEmpty(code) || String.IsNullOrEmpty(userId) || String.IsNullOrEmpty(pass))
                return -1;

            if (!System.IO.File.Exists(LoginFile))
            {
                return -1;
            }

            // read the user infos in the server side
            List<UserInfo> userInfos = new List<UserInfo>();

            var lines = System.IO.File.ReadAllLines(LoginFile).Where(o => o.StartsWith(MachineCode));
            foreach (string line in lines)
            {
                UserInfo info = ParseUserInfo(line);
                if (info != null)
                    userInfos.Add(info);
            }
            // check if the input {code, userId, pass} tuple is on the server side
            foreach (var userInfo in userInfos)
            {
                if (userInfo.MachineCode == code.Trim() &&
                    userInfo.UserId == userId.Trim() &&
                    userInfo.UserPass == pass.Trim())
                {
                    return 1;
                }
            }
            return -1;
        }

        private UserInfo ParseUserInfo(string line)
        {
            var items = line.Split(';');
            if (items.Length < 3)
                return null;

            UserInfo info = new UserInfo();
            // parse the code, userId and pass
            foreach (var item in items)
            {
                var pair = item.Split('='); // key value pair
                if (pair.Length != 2)
                    continue;
                string key = pair[0];
                string value = pair[1];

                switch (key)
                {
                    case MachineCode:
                        info.MachineCode = value;
                        break;
                    case UserId:
                        info.UserId = value;
                        break;
                    case UserPass:
                        info.UserPass = value;
                        break;
                }
            }
            return info;
        }

        private const string MachineCode = "MachineCode";
        private const string UserId = "userId";
        private const string UserPass = "userPw";

        /// <summary>
        /// VersionFile full path
        /// </summary>
        private string VersionFile
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["VersionFile"]; }
        }

        /// <summary>
        /// The LoginFile full path
        /// </summary>
        private string LoginFile
        {
            get { return System.Configuration.ConfigurationManager.AppSettings["LoginFile"]; }
        }
    }
}
